<%@LANGUAGE="VBSCRIPT"%> 
<!--#include file="functions.asp" -->
<%
'Dim cn
Dim sql
Dim sqlwhere
Dim rs2Excel
Dim A, fRegnum, fHide

'Set cn=Server.CreateObject("ADODB.Connection")
'cn.Open "MP2ENTTEST2", "sa", ""
   if (request("InvNum") <> "") then
   tmpSQL=" AND R.INVNUM LIKE '%"& request("InvNum") & "%'"
   end if
   
   if (request("InvStatus") <> "0") then
   tmpSQL= tmpSQL & " AND R.INVSTATUS='"& request("InvStatus") & "'"
   else
   tmpSQL= tmpSQL & " AND (R.INVSTATUS<>'PARKED' AND R.INVSTATUS<>'ROUTED') "
   end if
   
   if (request("CurSymbol") <> "N") then
   tmpSQL= tmpSQL & " AND R.CURRENCYSYMBOL='"& request("CurSymbol") & "'"
   end if   
      
   if (request("VendorID") <> "") then
   tmpSQL= tmpSQL & " AND R.VENDORID='"& request("VendorID") & "'"
   end if
   
   if (request("RefOrder") <> "0") then
   tmpSQL= tmpSQL & " AND R.REFORDER='"& request("RefOrder") & "'"
   end if
   
   if (Request("RecDate") <> "") Then
   Select Case Request("RptCond")
    Case 1
     tmpSQL = tmpSQL & " AND R.CONVERT(CHAR(10),INVRECDATE,101) = '" & Request("RecDate") & "'"
	Case 2
	 tmpSQL = tmpSQL & " AND R.INVRECDATE <= CONVERT(DATETIME,'" & Request("RecDate") & "',101)"
	Case 3
	 tmpSQL = tmpSQL & " AND R.INVRECDATE >= CONVERT(DATETIME,'" & Request("RecDate") & "',101)"
	Case 4
	 tmpSQL = tmpSQL & " AND R.INVRECDATE BETWEEN '" & Request("RecDate") & "' AND '" & Request("RecDate2") & "'"
   End Select
   End If
   
   dim tmpSort
   tmpSort="UPDATESTAMP"
   if (request("dSort") <> "") then
   tmpSort= request("dSort")
   end if
   
   dim tmpOrder
   tmpOrder = "ASC"
   if (request("dOrder") <>"") then
   tmpOrder= request("dOrder")
   end if
   Dim CurrOrder
   if (tmpOrder = "ASC" )then 
   CurrOrder ="DESC"
   else
   CurrOrder="ASC"
   end if
 %>
 

<%
if (isUC="Y") then
sql = "SELECT R.REGNUM, R.INVNUM, R.PONUM, R.PAYMENTDATE, VENDNAME = CASE WHEN V.VENDORBRANCHID IS NOT NULL THEN V.NAME + '  (' + V.VENDORBRANCHID + ')' " 
sql = sql & " WHEN V.VENDORBRANCHID IS NULL THEN V.NAME END, R.INVSTATUS, R.INVRECDATE, R.INVDUEDATE, "
sql = sql & " R.CURRENCYSYMBOL, R.USERDEPT,R.INVAMOUNT, C.ACCTCODE, C.T0CODE, C.T1CODE, C.T2CODE, C.T3CODE, C.AMOUNT " 
sql = sql & " FROM EXIREG R LEFT OUTER JOIN EXICHARGE C ON R.SEQNUM=C.INVSEQNUM LEFT OUTER JOIN VENDOR V ON R.VENDORID=V.VENDORID "
sql = sql & " WHERE  (R.USERCOORD='" & empCode & "' OR R.USERDEPT='" & Dept & "')  "
sql = sql & tmpSQL & " ORDER BY R."  & tmpSort & " " & tmpOrder 
else
sql = "SELECT R.REGNUM, R.INVNUM, R.PONUM, R.PAYMENTDATE, VENDNAME = CASE WHEN V.VENDORBRANCHID IS NOT NULL THEN V.NAME + '  (' + V.VENDORBRANCHID + ')' " 
sql = sql & " WHEN V.VENDORBRANCHID IS NULL THEN V.NAME END, R.INVSTATUS, R.INVRECDATE, R.INVDUEDATE, "
sql = sql & " R.CURRENCYSYMBOL, R.USERDEPT,R.INVAMOUNT, C.ACCTCODE, C.T0CODE, C.T1CODE, C.T2CODE, C.T3CODE, C.AMOUNT " 
sql = sql & " FROM EXIREG R LEFT OUTER JOIN EXICHARGE C ON R.SEQNUM=C.INVSEQNUM LEFT OUTER JOIN VENDOR V ON R.VENDORID=V.VENDORID "
sql = sql & " WHERE (R.VERIFIEDBY='" & empCode 
sql = sql & "' OR R.APPROVEDBY='" & empCode &"' OR APPROVED2BY='" & empCode & "' OR R.AUTHORISEDBY='" & empCode & "') "
sql = sql & tmpSQL & " ORDER BY R."  & tmpSort & " " & tmpOrder 
end if  

Set rs2Excel = cn.Execute(sql)
%>
<%
' Tells the browser to open excel
Response.ContentType = "application/vnd.ms-excel" 
%>
<html>
<head>
<title>Invoice List</title>
</head>
<body bgcolor="#FFFFFF">
<table width="2500" border="0" cellspacing="1" cellpadding="3" bgcolor="#333333">
  <tr bgcolor="#ffffff" style="font-weight:bold;" align="center"> 
    <td width="40">No.</td>
    <td width="134">Reg. No.</td>
    <td width="201">Invoice No.</td>
    <td width="145">PO/SO/CO</td>
    <td width="145">Dept</td>
    <td width="541">Vendor</td>
    <td width="106">Status</td>
    <td width="120"> Receive Dt</td>
    <td width="117">Due Date</td>
    <td width="115">Payment Dt</td>
    <td width="42">Curr</td>
    <td width="118">Inv. Amount</td>
    <td width="99">Acct. Code</td>
    <td width="91">T0 (Company)</td>
    <td width="101">T1(AFE)</td>
    <td width="100">T2 (Activity)</td>
    <td width="97">T3</td>
    <td width="127">AFE Amount</td>
  </tr>
  <% 
A = 1
fRegnum = ""
fHide = false
Do while not rs2Excel.Eof 
if fRegnum = CStr(rs2Excel("REGNUM")) then
  fHide = true
else  
  fHide = false
  fRegnum = CStr(rs2Excel("REGNUM"))
end if
%>
  <tr bgcolor="#ffffff"> 
    <td width="40" align="center"><%= A %></td>
    <td width="134"><% if fHide = false then response.Write("'" & fRegnum) end if %></td>
    <td width="201"><% if fHide = false then response.Write("'" & CStr(rs2Excel("INVNUM"))) end if %></td>
    <td width="145"><% if fHide = false then response.Write(rs2Excel("PONUM")) end if %></td>
    <td width="145"><% if fHide = false then response.Write(rs2Excel("USERDEPT")) end if %></td>
    <td width="541"><% if fHide = false then response.Write(rs2Excel("VENDNAME")) end if %></td>
    <td width="106" align="center"><% if fHide = false then response.Write(rs2Excel("INVSTATUS")) end if %></td>
    <td width="120" align="center"><% if fHide = false then response.Write(rs2Excel("INVRECDATE")) end if %></td>
    <td width="117" align="center"><% if fHide = false then response.Write(rs2Excel("INVDUEDATE")) end if %></td>
    <td width="115" align="center"><% if fHide = false then response.Write(rs2Excel("PAYMENTDATE")) end if %></td>
    <td width="42" align="center"><% if fHide = false then response.Write(rs2Excel("CURRENCYSYMBOL")) end if %></td>
    <td width="118"><% if fHide = false then response.Write(rs2Excel("INVAMOUNT")) end if %></td>
    <td width="99" bgcolor="#ffffff" align="center"><%= rs2Excel("ACCTCODE") %></td>
    <td width="91" bgcolor="#ffffff" align="center"><%= rs2Excel("T0CODE") %></td>
    <td width="101" bgcolor="#ffffff" align="center"><%= rs2Excel("T1CODE") %></td>
    <td width="100" bgcolor="#ffffff" align="center"><%= rs2Excel("T2CODE") %></td>
    <td width="97" bgcolor="#ffffff" align="center"><%= rs2Excel("T3CODE") %></td>
    <td width="127" bgcolor="#ffffff"><%= rs2Excel("AMOUNT") %></td>
  </tr>
  <% A = A + 1
rs2Excel.MoveNext

Loop  
%>
</table>
</body>
</html>
